package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    //更新
    public void update(UserInfo info) {
        userInfoMapper.updateById(info);
    }

    //根据id查询
    public UserInfo findById(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    //根据用户id集合，查询所有用户信息
    public Map<Long, UserInfo> findByIds(List<Long> userIds) {
        if(userIds == null) {
            return new HashMap<>();
        }
        //查询
        QueryWrapper<UserInfo> qw = new QueryWrapper<>();
        qw.in("id",userIds);
        List<UserInfo> list = userInfoMapper.selectList(qw);
        //转化map
        Map<Long, UserInfo> map = new HashMap<>();
        for (UserInfo info : list) {
            map.put(info.getId(),info);
        }
        return map;
    }

    //分页
    @Override
    public IPage findAll(Integer page, Integer pagesize) {
        //1、构造分页参数对象Page
        Page<UserInfo> pageParam = new Page<>(page,pagesize);
        //2、构建查询条件（略）
        return userInfoMapper.selectPage(pageParam,null);
    }

    @Override
    public UserInfo select(Long userID) {
        return  userInfoMapper.selectById(userID);
    }


}
